<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt">

  <xsl:variable name="apos">
    <xsl:text>'</xsl:text>
  </xsl:variable>

  <xsl:param name="rootUrl" />
	<xsl:param name="id" />
	<xsl:param name="desiredThumbnailWidth" select="100" />
	<xsl:param name="desiredThumbnailHeight" select="75" />

	<xsl:decimal-format name="en" decimal-separator="." grouping-separator=","/>


  <xsl:template match="/">
	<xsl:choose>
		<xsl:when test="$id != ''">
			<xsl:apply-templates select="//category[@id=$id]">
				<xsl:sort order="ascending" select="@id"/>
			</xsl:apply-templates>
		</xsl:when>
		<xsl:otherwise>
			<xsl:apply-templates select="/website/inventory/category"></xsl:apply-templates>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

	<xsl:template match="category">
		<xsl:for-each select="ancestor-or-self::category">
			<h2>
				<xsl:element name="a">
          <xsl:attribute name="href">
            <xsl:call-template name="contentLink">
              <xsl:with-param name="content" select="ancestor-or-self::node()" />
            </xsl:call-template>
          </xsl:attribute>
					<xsl:value-of select="@title"/>
				</xsl:element>
			</h2>
		</xsl:for-each>
		<xsl:value-of select="text()"/>
		<xsl:if test="./category">
			<div id="categorylist">
				<xsl:for-each select="./category">
					<xsl:call-template name="categoryItem" />
				</xsl:for-each>
			</div>
		</xsl:if>
		<xsl:if test="./product">
			<div id="productlist">
				<xsl:apply-templates select="./product">
					<xsl:sort select="@id" order="ascending" data-type="number"/>
				</xsl:apply-templates>
			</div>
		</xsl:if>
	</xsl:template>

  <xsl:template name="contentLink">
    <xsl:param name="content" />
    <xsl:for-each select="$content">
      <xsl:if test="@key != ''">
        <xsl:choose>
        <xsl:when test="count(descendant::node()) > 0 and not(position()=last())">/<xsl:value-of select="@key"/></xsl:when>
          <xsl:otherwise>/<xsl:value-of select="@key"/>.t</xsl:otherwise>
        </xsl:choose>
      </xsl:if>
    </xsl:for-each>
    
  </xsl:template>

	<xsl:template name="categoryItem">
		<xsl:element name="a">
			<xsl:attribute name="href">
        <xsl:call-template name="contentLink">
          <xsl:with-param name="content" select="ancestor-or-self::node()" />
        </xsl:call-template>
			</xsl:attribute>
			<div class="category">
				<div class="photo">
					<xsl:element name="a">
						<xsl:attribute name="href">
              <xsl:call-template name="contentLink">
                <xsl:with-param name="content" select="ancestor-or-self::node()" />
              </xsl:call-template>
						</xsl:attribute>
						<xsl:element name="img">
							<xsl:choose>
								<xsl:when test="@thumbnail">
									<xsl:attribute name="src">
										/services/thumbnail.aspx?i=<xsl:value-of select="@thumbnail"/>&amp;sy=<xsl:value-of select="$desiredThumbnailHeight"/>&amp;sx=<xsl:value-of select="$desiredThumbnailWidth"/>&amp;method=restricttosize
                  </xsl:attribute>
								</xsl:when>
								<xsl:otherwise>
									<xsl:attribute name="src">
                    /services/thumbnail.aspx?i=<xsl:value-of select="@PhotoURL"/>&amp;sy=<xsl:value-of select="$desiredThumbnailHeight"/>&amp;sx=<xsl:value-of select="$desiredThumbnailWidth"/>&amp;method=restricttosize
                  </xsl:attribute>
								</xsl:otherwise>
							</xsl:choose>
							<xsl:attribute name="border">0</xsl:attribute>
						</xsl:element>
					</xsl:element>
				</div>
				<div class="title">
					<xsl:element name="a">
						<xsl:attribute name="href">
              <xsl:call-template name="contentLink">
                <xsl:with-param name="content" select="ancestor-or-self::node()" />
              </xsl:call-template>
						</xsl:attribute>

						<xsl:value-of select="@title"/>
					</xsl:element>
				</div>
			</div>
		</xsl:element>

		</xsl:template>

	<xsl:template match="product">
		<xsl:element name="a"><xsl:attribute name="href"><xsl:value-of select="$rootUrl"/>?guid=03<xsl:value-of select="@guid"/></xsl:attribute>
			<div class="product">
				<div class="photo">
					<xsl:element name="a">
						<xsl:attribute name="href">
              <xsl:call-template name="contentLink">
                <xsl:with-param name="content" select="ancestor-or-self::node()" />
              </xsl:call-template>
						</xsl:attribute>
						<xsl:element name="img">
							<xsl:choose>
								<xsl:when test="@thumbnail">
									<xsl:attribute name="src">
                    /services/thumbnail.aspx?i=<xsl:value-of select="@thumbnail"/>&amp;sy=<xsl:value-of select="$desiredThumbnailHeight"/>&amp;sx=<xsl:value-of select="$desiredThumbnailWidth"/>&amp;method=restricttosize
                  </xsl:attribute>
								</xsl:when>
								<xsl:otherwise>
									<xsl:attribute name="src">
                    /services/thumbnail.aspx?i=<xsl:value-of select="@PhotoURL"/>&amp;sy=<xsl:value-of select="$desiredThumbnailHeight"/>&amp;sx=<xsl:value-of select="$desiredThumbnailWidth"/>&amp;method=restricttosize
                  </xsl:attribute>
								</xsl:otherwise>
							</xsl:choose>
							<xsl:attribute name="border">0</xsl:attribute>
						</xsl:element>
					</xsl:element>
				</div>
				<div class="title">
					<xsl:element name="a">
						<xsl:attribute name="href">
              <xsl:call-template name="contentLink">
                <xsl:with-param name="content" select="ancestor-or-self::node()" />
              </xsl:call-template>
						</xsl:attribute>
						<xsl:value-of select="@title"/>
					</xsl:element>
				</div>
				<xsl:if test="number(@Price) != 0">
				<xsl:if test="number(@Discount) != 0">
					<div class="price">
						$<xsl:value-of select="format-number(number(@Price),'#,###.00','en')"/>
					</div>
				</xsl:if>
				<xsl:if test="number(@Discount) = 0">
					<div class="price">
						$<xsl:value-of select="format-number(number(@Price)-number(@Discount),'#,###.00','en')"/>
					</div>
				</xsl:if>
				</xsl:if>
			</div>
		</xsl:element>
	</xsl:template>

</xsl:stylesheet> 

